package com.dingreading.cloud.admin.util;

import com.alibaba.fastjson2.JSONObject;
import com.dingreading.cloud.admin.dto.HeadStaffDto;
import com.dingreading.cloud.admin.service.AdminRoleStaffService;
import com.dingreading.cloud.admin.service.HeadStaffService;
import com.dingreading.cloud.admin.util.redis.RedisService;
import com.dingreading.cloud.common.util.JsonUtil;
import com.dingreading.cloud.common.util.RedisParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class HeadStaffUtil {

    @Resource
    private HeadStaffService headStaffService;
    @Resource
    private AdminRoleStaffService roleStaffService;
    @Resource
    protected RedisService redisService;

    /**
     * 根据总部员工信息
     */
    public HeadStaffDto getLoginInfo(String staffUid) {
        String key = RedisParam.HEAD_STAFF + staffUid;
        String value = (String) redisService.get(key);
        HeadStaffDto staff = null;
        if (StringUtils.isBlank(value)) {
            staff = headStaffService.dtoGetByUid(staffUid);
            if (staff != null) {
                List<String> identifyList = roleStaffService.identifyListByStaffUid(staff.getUid());
                if (CollectionUtils.isNotEmpty(identifyList)) {
                    staff.setIdentifyList(identifyList);
                }
                redisService.set(key, JsonUtil.toJsonString(staff), RedisParam.HOUR_TIME);
            }
        } else {
            staff = JSONObject.parseObject(value, HeadStaffDto.class);
        }
        return staff;
    }

}
